﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using ChanExt;
using C.Entities;
using C.Unite;

namespace ArtLand.OA.Project.SystemMgr
{
    public partial class OA_DepartmentChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropType();
                LoadData();

            }

        }

        private void DropType()
        {
            List<OA_Department> items = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.TopID == 0).OrderBy(OA_Department._.OrderID.Asc).ToList();
            foreach (OA_Department row in items)
            {
                int count = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.TopID == row.DepID).OrderBy(OA_Department._.OrderID.Asc).Count();
                ListItem li = new ListItem();
                li.Text = row.DepName;
                li.Value = row.DepID.ToString();
                //if (count > 0)
                //{
                //    li.EnableSelect = false;

                //}
                DrpSort.Items.Add(li);
                if (count > 0)
                {
                    ResolveSubTree(row.DepID, 0);
                }
            }
            ListItem li2 = new ListItem();
            li2.Text = "--所属类别--";
            li2.Value = "0";
            li2.EnableSelect = false;
            DrpSort.Items.Insert(0, li2);
            if (!string.IsNullOrEmpty(Request["pid"]))
            {

                DrpSort.SelectedValue = Request["pid"];
            }
        }
        private void ResolveSubTree(int id, int Leve)
        {
            Leve++;
            List<OA_Department> items = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.TopID == id).OrderBy(OA_Department._.OrderID.Asc).ToList();
            if (items.Count > 0)
            {
                foreach (OA_Department row in items)
                {
                    int count = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.TopID == row.DepID).OrderBy(OA_Department._.OrderID.Asc).Count();
                    ListItem li = new ListItem();
                    li.Text = row.DepName;
                    li.Value = row.DepID.ToString();
                    li.SimulateTreeLevel = Leve;
                    //if (count > 0)
                    //{
                    //    li.EnableSelect = false;
                    //}
                    DrpSort.Items.Add(li);
                    if (count > 0)
                    {
                        ResolveSubTree(row.DepID, Leve);
                    }

                }
            }

        }



        private void LoadData()
        {

            // 关闭按钮的客户端脚本：
            // 1. 首先确认窗体中表单是否被修改（如果已经被修改，则弹出是否关闭的确认对话框）
            // 2. 然后关闭本窗体，回发父窗体
            btnClose.OnClientClick = ActiveWindow.GetConfirmHidePostBackReference();
            int id = 0;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                id = int.Parse(Request["id"]);
            }
            if (id > 0)
            {
                OA_Department tm = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.DepID == int.Parse(Request["id"])).ToFirst();
                txt_ModeuleName.Text = tm.DepName;
                DrpSort.SelectedValue = tm.TopID.ToString();
                txt_orderid.Text = tm.OrderID.ToString();
            }

            #region old code
            // 1.第一种调用方法
            //string confirmText = "是否确认关闭当前页？<br/>您在当前页所做的修改没有保存。继续编辑当前页，请选择“取消”。<br/>选择“确定”关闭当前页，选择“取消”继续编辑当前页。";
            //string closeScript = CurrentActiveWindow.GetCloseReference();
            //string confirmScript = ExtAspNet.Confirm.GetShowReference(confirmText, "确认关闭", ExtAspNet.MessageBoxIcon.Warning, closeScript, "return false;");
            //btnClose.OnClientClick = String.Format("if({0}){{{1}}}else{{{2}}}", ExtAspNet.PageContext.GetPageStateChangedReference(), confirmScript, closeScript);

            //// 2.第二种调用方法
            //string confirmText = "是否确认关闭当前页？<br/>您在当前页所做的修改没有保存。继续编辑当前页，请选择“取消”。<br/>选择“确定”关闭当前页，选择“取消”继续编辑当前页。";
            //string closeScript = CurrentActiveWindow.GetCloseReference();
            //btnClose.OnClientClick = ExtAspNet.PageContext.GetPageStateChangedConfirmReference("确认关闭", confirmText, closeScript, "return false;");

            //// 3.第三种调用方法（并且在父页面中可以简单的 window1.OnClientCloseButtonClick = window1.GetIFramePageStateChangedFunction(); 来注册窗口右上角关闭按钮动作[即先判断IFrame中表单内容是否变化]）
            //PageContext.RegisterPageStateChangedStartupScript(); // 注意这个语句应该放在if的外面，否则回发时会出错
            //btnClose.OnClientClick = PageContext.GetPageStateChangedFunction();

            // 4.第四种调用方法（推荐的做法，页面中的PageManager控件增加属性RegisterPageStateChangedScript="true"，并且在父页面中可以简单的 window1.OnClientCloseButtonClick = window1.GetIFramePageStateChangedFunction(); 来注册窗口右上角关闭按钮动作[即先判断IFrame中表单内容是否变化]）
            //btnClose.OnClientClick = PageContext.GetConfirmFormModifiedReference(); 
            #endregion
        }


        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            OA_Department tm = new OA_Department();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                tm.DepID = int.Parse(Request["id"]);
                tm.Attach();
                tm.DepName = txt_ModeuleName.Text;
                tm.TopID = int.Parse(DrpSort.SelectedValue);
                tm.OrderID = int.Parse(txt_orderid.Text);
                tm.DepPath = Getpath(int.Parse(DrpSort.SelectedValue));
                returnvalue = TbsDbSession.Default.Update<OA_Department>(tm);

            }
            else
            {
                tm.DeAttach();
                tm.DepName = txt_ModeuleName.Text;
                tm.TopID = int.Parse(DrpSort.SelectedValue);
                tm.OrderID = int.Parse(txt_orderid.Text);
                tm.DepPath = Getpath(int.Parse(DrpSort.SelectedValue));
                returnvalue = TbsDbSession.Default.Insert<OA_Department>(tm);
            }
            if (returnvalue > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
        }
        public string Getpath(int sortid)
        {
            string n = "0";
            OA_Department ttts = TbsDbSession.Default.From<OA_Department>().Where(OA_Department._.DepID == sortid).ToFirst();
            if (ttts != null)
            {
                if (ttts.DepPath == "0")
                {
                    n = "0|" + sortid.ToString() + "|";
                }
                else
                {
                    n = ttts.DepPath + sortid.ToString() + "|";
                }
            }
            else
            {
                n = "0|";
            }
            return n;
        }

    }
}